<template>
  <a-modal
    :title="title"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-decorator="['id']"/>
        </a-form-item>

        <a-form-item
          label="声卡名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入声卡名称" v-decorator="['cardName', {rules: [{required: true, message: '请输入声卡名称！'}]}]"/>
        </a-form-item>

        <a-form-item
          label="唯一编码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input :disabled="true" v-decorator="['code', {rules: [{required: true, message: '请输入唯一编码！'}]}]"/>
        </a-form-item>

        <a-form-item
          label="声卡识别名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入声卡识别名称"
                   v-decorator="['distinguishCode', {rules: [{required: true, message: '请输入声卡识别名称！'}]}]"/>
        </a-form-item>

        <a-form-item
          label="通道数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-radio-group v-decorator="['vocalTractNumber',{initialValue:1}]">
            <a-radio :value="1">单声道</a-radio>
            <a-radio :value="2">双声道</a-radio>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { edit } from '@/api/modular/main/soundCard'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        title: ''
      }
    },
    created () {
    },
    methods: {
      edit (record) {
        this.visible = true
        this.title = '编辑' + record.cardName
        setTimeout(() => {
          this.form.setFieldsValue({
            id: record.id,
            cardName: record.cardName,
            code: record.code,
            distinguishCode: record.distinguishCode,
            vocalTractNumber: record.vocalTractNumber
          })
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        validateFields((errors, values) => {
          if (!errors) {
            this.confirmLoading = true
            edit(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('编辑成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
